package com.bungarus.group.cachestore;

import java.io.Serializable;
import java.util.Objects;
import java.util.Set;

public class StoreRow implements Serializable {
    public final static String SEPERATOR = ":";
    private String tableName;
    private String rowUniqueKeyValue;

    private Set<StoreColumn> rowValues;

    public String getCacheKey() {
        return this.getTableName() + SEPERATOR + this.getRowUniqueKeyValue();
    }

    static public Builder newBuilder() {
        return new Builder();
    }

    @Override
    public String toString() {
        return "StoreRow{" +
                "tableName='" + tableName + '\'' +
                ", rowUniqueKeyValue='" + rowUniqueKeyValue + '\'' +
                ", rowValues=" + rowValues +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        StoreRow storeRow = (StoreRow) o;
        return Objects.equals(getTableName(), storeRow.getTableName()) &&
                Objects.equals(getRowUniqueKeyValue(), storeRow.getRowUniqueKeyValue());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getTableName(), getRowUniqueKeyValue());
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getRowUniqueKeyValue() {
        return rowUniqueKeyValue;
    }

    public void setRowUniqueKeyValue(String rowUniqueKeyValue) {
        this.rowUniqueKeyValue = rowUniqueKeyValue;
    }

    public Set<StoreColumn> getRowValues() {
        return rowValues;
    }

    public void setRowValues(Set<StoreColumn> rowValues) {
        this.rowValues = rowValues;
    }

    public static class Builder {
        private StoreRow row = new StoreRow();

        private Builder() {}

        public Builder table(String tableName) {
            row.setTableName(tableName);
            return this;
        }

        public Builder id(String idValue) {
            row.setRowUniqueKeyValue(idValue);
            return this;
        }

        public Builder values(Set<StoreColumn> values) {
            row.setRowValues(values);
            return this;
        }

        public StoreRow build() {
            return row;
        }
    }
}
